package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoCacheService;
import cn.wolfcode.luowowo.cache.service.IVerifyCodeCacheService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.Assert;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.fastjson.JSON;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Reference
    private IVerifyCodeCacheService verifyCodeCacheService;
    @Reference
    private IUserInfoCacheService userInfoCacheService;

    @Reference
    private ITravelService travelService;

    @Reference
    private IDestinationService destinationService;

    public boolean checkPhone(String phone) {
        //返回null就表示该手机号码可以注册
        return userInfoMapper.checkPhone(phone) == null;
    }

    @Override
    public AjaxResult userRegist(String verifyCode, String phone, String nickname, String password, String rpassword) {
        AjaxResult result = new AjaxResult();
        try {
            //数据验证
            Assert.hasLength(verifyCode, "验证码不能为空");
            Assert.hasLength(phone, "手机号不能为空");
            Assert.hasLength(nickname, "昵称不能为空");
            Assert.hasLength(password, "密码不能为空");
            Assert.hasLength(rpassword, "确认密码不能为空");

            //判断验证码是否一致
            String vCode = verifyCodeCacheService.getVerifyCode(phone);
            Assert.hasLength(vCode, "验证码错误");//如果输入验证码的时候已经失效, 这里就要再进行判断一次
            Assert.equalsIgnoreCase(verifyCode, vCode, "验证码错误");

            //判断密码是否一致
            Assert.equals(password, rpassword, "密码不一致");

            //把信息封装成对象
            UserInfo userInfo = new UserInfo();
            userInfo.setNickname(nickname);
            userInfo.setPhone(phone);
            userInfo.setPassword(password);
            userInfo.setEmail("xxx@163.com");
            userInfo.setLevel(1);
            userInfo.setCity("广州");
            userInfo.setBirthday(new Date());
            userInfo.setHeadImgUrl("/images/default.png");
            userInfo.setInfo("sss");

            //注册新用户
            userInfoMapper.insert(userInfo);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    public AjaxResult userLogin(String phone, String password) {
        AjaxResult result = new AjaxResult();
        UserInfo user = userInfoMapper.checkUserInfo(phone,password);
        //登录失败
        if (user == null){
            result.mark("账号和密码不一致");
            return result;
        }

        //登录成功,缓存用户信息
        String token = UUID.randomUUID().toString().replace("-", "");
        userInfoCacheService.setUserInfoCache(token, JSON.toJSONString(user));

        //传递token给控制器,让控制器返回cookie到浏览器
        result.setData(token);
        return result;
    }

    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }

    public UserInfo getById(Long userInfoId) {
        return userInfoMapper.selectByPrimarKey(userInfoId);
    }

    public void addVisitor(Long uid, Long id) {
        userInfoMapper.addVisitor(uid,id);
    }

    public List<UserInfo> listvisitor(Long uid) {
        List<UserInfo> list = userInfoMapper.listvisitor(uid);
        return list;
    }

    public Integer gettotal(Long uid) {
        return userInfoMapper.gettotal(uid);
    }

    public Integer nowdayTotal(Long uid) {
        return userInfoMapper.nowdayTotal(uid);
    }

    @Override
    public Integer getAttentionNumById(Long uid) {

        return userInfoMapper.getAttentionNumById(uid);
    }

    @Override
    public Integer getFansNumById(Long uid) {
        return userInfoMapper.getFansNumById(uid);
    }

    @Override
    public List<UserInfo> getAttentionUser(Long uid) {
        UserInfo userInfo = userInfoMapper.getAttentionUser(uid);
        return userInfo!=null?userInfo.getAttentionUser() : null;
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.update(userInfo);
    }

    @Override
    public Integer getIntegrity(Long id) {
        UserInfo userInfo = userInfoMapper.selectByPrimarKey(id);
        int num=0;
        if(userInfo.getNickname()!=null&&!"".equals(userInfo.getNickname())){
            num+=1;
        }
        if(userInfo.getGender()!=null){
            num+=1;
        }
        if(userInfo.getCity()!=null&&!"".equals(userInfo.getCity())){
            num+=1;
        }
        if(userInfo.getBirthday()!=null){
            num+=1;
        }
        if(userInfo.getInfo()!=null&&!"".equals(userInfo.getInfo())){
            num+=1;
        }
        BigDecimal num1 = new BigDecimal(Integer.toString(num));
        BigDecimal num2 = new BigDecimal(Integer.toString(5));
        BigDecimal divide = num1.divide(num2, 2, BigDecimal.ROUND_HALF_UP);
        Double aDouble = Double.valueOf(divide.toString());
        double v = aDouble * 100;
        String s = String.valueOf(v);
        return Integer.valueOf(s.substring(0,s.indexOf(".")));
    }

    @Override
    public void updateHeadImg(Long userInfoId, String upload) {
        upload="/"+upload;
        userInfoMapper.updateHeadImg(userInfoId,upload);
    }

    @Override
    public AjaxResult verifyPhone(Long id, String phone) {
        AjaxResult result = new AjaxResult();
        if(id==null){
            return result.mark("请登录");
        }
        String userPhone=userInfoMapper.getPhoneById(id);
        if(!phone.equals(userPhone)){
            return result.mark("手机验证失败");
        }

        return result;
    }

    @Override
    public AjaxResult changePW(Long id, String password) {
        AjaxResult result = new AjaxResult();
        userInfoMapper.changePassword(id,password);
        return result;
    }

    @Override
    public AjaxResult verifyEmail(Long id, String email) {
        AjaxResult result = new AjaxResult();
        if(id==null){
            return result.mark("请登录");
        }
        String userEmail=userInfoMapper.getEmailById(id);
        if(!email.equals(userEmail)){
            return result.mark("邮箱验证失败");
        }

        return result;
    }

    @Override
    public AjaxResult bindingEmail(Long id, String email) {
        AjaxResult result = new AjaxResult();
        if(id==null){
            return result.mark("请登录");
        }
        String userEmail=userInfoMapper.getEmailById(id);
        if(userEmail!=null&&!"".equals(userEmail)){
            return result.mark("邮箱已经绑定,请联系客服改绑");
        }else {
            userInfoMapper.changeEmail(id,email);
        }

        return result;
    }

    @Override
    public AjaxResult changePhone(Long id, String phone) {
        AjaxResult result = new AjaxResult();
        userInfoMapper.changePhone(id,phone);
        return result;
    }

    @Override
    public AjaxResult verifyPEP(Long id, String phone, String email, String password) {
        AjaxResult result = new AjaxResult();
        try {
            Assert.hasLength(phone, "手机号不能为空");
            Assert.hasLength(email, "邮箱不能为空");
            Assert.hasLength(password, "密码不能为空");

            UserInfo userInfo = userInfoMapper.selectById(id);
            System.out.println(userInfo);
            if(!phone.equals(userInfo.getPhone())){
                throw new RuntimeException("手机号不一致");
            }
            if(!email.equals(userInfo.getEmail())){
                throw new RuntimeException("邮箱不一致");
            }
            if(!password.equals(userInfo.getPassword())){
                throw new RuntimeException("密码不一致");
            }
            userInfoMapper.deleteUserInfo(id);

        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }

        return result;
    }


}
